**************************************************************************************
	;Header frs File-linken

	IFD	Flag_project
Flag_romhead	set 1
	ENDC
	IFND	Flag_romhead
Flag_romhead	set 0
	ENDC
	IFEQ	Flag_romhead
Flag_romhead	set 1
	include	Nutz_project
	ELSEIF
**************************************************************************************

	;RomHead

	dc.l	$0				;Reset Stackpointer
	dc.l	reset_entry			;Reset Einsprung
	IFNE	Exceptions
	dc.l	Exception2
	dc.l	Exception3
	dc.l	Exception4
	dc.l	Exception5
	dc.l	Exception6
	dc.l	Exception7
	dc.l	Exception8
	dc.l	Exception9
	dc.l	Exception10
	dc.l	Exception11
	ENDC
	ds.b	$64-*
	dc.l	int1
	dc.l	int2
	dc.l	int3
	dc.l	Int_HBlank+WorkRam
	dc.l	int5
	dc.l	Int_VBlank+WorkRam
	ds.b	$80-*
	dc.l	0
	ds.l	($100-*)/4

***********************************************************************************************
*	$100, SEGA Rom-Header								      *
***********************************************************************************************

	dc.b	"SEGA MEGA DRIVE "
	dc.b	"(C)SEGA 1988.JUL"
	dc.b	"MR NUTZ HOPPIN MAD                              "
	dc.b	"MR NUTZ HOPPIN MAD                              "
	dc.b	"GM 00000001-00"
	dc.w	0							;Checksum
	dc.b	"J               "					;Control Data
	dc.l	$000000,$100000
	dc.l	$ff0000,$ffffff
	dc.b	"            "						;External Ram
	dc.b	"            "						;Modem Data
	dc.b	"                                        "		;MEMO
	dc.b	"JUE             "					;Contries

***********************************************************************************************
*	$200, Start des Programms							      *
***********************************************************************************************
reset_entry:
	move.b	$a10001,d0		;SEGA Key
	andi.b	#$0f,d0			;  "   "
	beq.s	.skip			;  "   "
	move.l	#'SEGA',$a14000		;  "   "
.skip:

.dma_security:
	btst	#1,$c00005		;DMA security
	bne.s	.dma_security		;Wenn DMA luft, warten bis sie stoppt!

	lea	Stack,a7		;Stack initialisieren
	bsr	Init_Hardware		;Alle Register presetten
	bsr	Init_Interupts		;Interrupts laufbar machen
	jsr	TFMX+0			;Z80 initialisieren.
	move.w	#$2300,sr		;Interrupts zulassen

;----------------------------------------------------------------------------------------------
;	Typ des SEGAs feststellen, Lockout-screen Anzeigen und Music-Player initen	      -
;----------------------------------------------------------------------------------------------

	moveq	#0,d7
	move.b	$a10001,d7
	moveq	#0,d0			;NTSC Flag
	btst	#6,d7
	beq.s	.ntsc
	moveq	#1,d0			;PAL Flag
.ntsc:
	jsr	TFMX+20			;NTSC/PAL initialisierung der Musik-routine

	and.b	#$c0,d7
	rol.b	#2,d7

	lea	Config_Table(pc),a0
	move.b	(a0,d7.w),d0		;Diese Kennung htte das vorliegende SEGA.

	lea	$1f0(pc),a0		;Kommt diese Kennung in der Liste vor??
	moveq	#16-1,d7
.desperately_seeking:
	cmp.b	(a0)+,d0
	beq.s	.start_game		;Jawoll! Wir ham's geschafft!
	dbf	d7,.desperately_seeking

.Show_Lockout:
	moveq	#0,d0			;NEIN! Das Spiel darf nicht laufen.
	jmp	Mickey.library

;----------------------------------------------------------------------------------------------
;	Game starten									      -
;----------------------------------------------------------------------------------------------

.start_game:
	IFNE	DoIntro
	lea	Start(pc),a0
	moveq	#1,d0			;Code fr Game-Start
	jmp	Mickey.library
	ELSEIF
	jmp	Start
	ENDC

Config_Table:	dc.b	"JXUE"

;--------------------------------------------------
; Display OFF without missing VBL & HBL IRQs
;
black_screen:
	lea	$c00000,a0
	move.w	#$8f02,4(a0)
	move.l	#$c0000000,4(a0)
	moveq	#0,d0
	moveq	#15,d1
.set_colors:
	btst	#1,4(a0)
	beq.s	.set_colors
	move.l	d0,(a0)
	move.l	d0,(a0)
	dbf	d1,.set_colors
RoutRts:
	rts

;----------------------------------------------------------------------------------------------
;	Trap-Handler Debug-Use Only							      -
;----------------------------------------------------------------------------------------------
	IFNE	Exceptions
Exception2:
	addq.l	#8,sp
	move.w	#2,$ff8000
	bra	Trap_Handler
Exception3:
	addq.l	#8,sp
	move.w	#3,$ff8000
	bra	Trap_Handler
Exception4:
	move.w	#4,$ff8000
	bra	Trap_Handler
Exception5:
	move.w	#5,$ff8000
	bra	Trap_Handler
Exception6:
	move.w	#6,$ff8000
	bra	Trap_Handler
Exception7:
	move.w	#7,$ff8000
	bra	Trap_Handler
Exception8:
	move.w	#8,$ff8000
	bra	Trap_Handler
Exception9:
	move.w	#9,$ff8000
	bra	Trap_Handler
Exception10:
	move.w	#10,$ff8000
	bra	Trap_Handler
Exception11:
	move.w	#11,$ff8000
	bra	Trap_Handler

Trap_Handler:
	move.l	d0,$ff8004
	move.l	d1,$ff8008
	move.l	d2,$ff800c
	move.l	d3,$ff8010
	move.l	d4,$ff8014
	move.l	d5,$ff8018
	move.l	d6,$ff801c
	move.l	d7,$ff8020

	move.l	a0,$ff8024
	move.l	a1,$ff8028
	move.l	a2,$ff802c
	move.l	a3,$ff8030
	move.l	a4,$ff8034
	move.l	a5,$ff8038
	move.l	a6,$ff803c
	move.l	a7,$ff8040
.dma_security:
	btst	#1,$c00005		;DMA security
	bne.s	.dma_security		;Wenn DMA luft, warten bis sie stoppt!

	bsr	Init_Interupts		;Interrupts laufbar machen
	bsr	Init_Hardware		;Alle Register presetten

	move.w	#$8164,$c00004

	move.w	#$8f02,$c00004
	lea	$c00000,a0
	lea	Not_Coltab(pc),a1
	move.l	#$c0000000,4(a0)

	movem.l	(a1)+,d0-d7
	move.l	d0,(a0)
	move.l	d1,(a0)
	move.l	d2,(a0)
	move.l	d3,(a0)
	move.l	d4,(a0)
	move.l	d5,(a0)
	move.l	d6,(a0)
	move.l	d7,(a0)

	moveq	#0,d0
	move.l	d0,(a0)
	move.l	d0,(a0)
	move.l	d0,(a0)
	move.l	d0,(a0)
	move.l	d0,(a0)
	move.l	d0,(a0)
	move.l	d0,(a0)
	move.l	d0,(a0)

	move.w	#$8f02,$c00004
	lea	Numbers_raw(pc),a0
	move.l	#$40000000,$c00004	;$0000
	move.w	#(512/2)-1,d7
.copyloop:
	move.w	(a0)+,$c00000
	dbf	d7,.copyloop
	moveq	#32/2-1,d7
.clearloop1:
	move.w	#$0000,$c00000
	dbf	d7,.clearloop1

	move.w	#$8f02,$c00004
	move.l	#$60000003,$c00004	;$e000
	move.w	#1024-1,d7
.copyloop2:
	move.w	#$8010,$c00000
	dbf	d7,.copyloop2

	move.l	#$40000003,$c00004	;$c000
	move.w	#1024-1,d7
.copyloop3:
	move.w	#$2000,$c00000
	dbf	d7,.copyloop3

	move.w	#$9000,$c00004

	move.l	#$61100003,$c00004

	move.w	$ff8000,d0
	bsr	Write_Byte

	move.w	#$8010,$c00000

	move.l	2(sp),d0		;Adresse
	bsr	Write_Long

	moveq	#8-1,d7
	move.l	#$61900003,d6
	lea	$ff8004,a0
	moveq	#0,d5
.loop:
	move.l	d6,$c00004
	move.w	#$800d,$c00000
	move.w	d5,d0
	bsr	NibbleOut
	move.w	#$2000,$c00000

	move.l	(a0)+,d0
	bsr	Write_Long
	add.l	#$00400000,d6
	addq.w	#1,d5
	dbf	d7,.loop
	add.l	#$00400000,d6

	moveq	#8-1,d7
	moveq	#0,d5
.loop2:
	move.l	d6,$c00004
	move.w	#$800a,$c00000
	move.w	d5,d0
	bsr	NibbleOut
	move.w	#$2000,$c00000

	move.l	(a0)+,d0
	bsr	Write_Long
	add.l	#$00400000,d6
	addq.w	#1,d5
	dbf	d7,.loop2

	move.w	#$8570,$c00004

	move.l	#$70000003,$c00004	;$e000
	move.w	#$0000,$c00000
	move.w	#$0000,$c00000

	move.l	#$40000010,$c00004
	move.w	#$0000,$c00000
	move.w	#$0000,$c00000

.forever:
	bra	.forever
NibbleOut:
	and.w	#$0f,d0
	or.w	#$8000,d0
	move.w	d0,$c00000
	rts

Write_Long:
	move.l	d0,d2
	rol.l	#8,d2
	move.l	d2,d0
	bsr	Write_Byte
	rol.l	#8,d2
	move.l	d2,d0
	bsr	Write_Byte
	rol.l	#8,d2
	move.l	d2,d0
	bsr	Write_Byte
	rol.l	#8,d2
	move.l	d2,d0
	bra	Write_Byte

Write_Byte:
	move.b	d0,d1
	and.w	#$f0,d0
	lsr.w	#4,d0
	or.w	#$8000,d0
	move.w	d0,$c00000
	and.w	#$0f,d1
	or.w	#$8000,d1
	move.w	d1,$c00000
	rts

Not_Coltab:
	dc.w	$000,$444,$666,$888,$eee,$eee,$eee,$eee
	dc.w	$eee,$eee,$eee,$eee,$eee,$eee,$eee,$eee

	ENDC

	IFNE	Exceptions+Coords
Numbers_raw:
	incbin	Work:SEGA/Sources/Mr_Nutz/Binary/Numbers.debug
	ENDC

	ENDC
